// import request from "@/utils/request";

// 封装webSocket对象
export class WebSocketConstructor {
  private websocket: WebSocket | null = null
  private readonly url: string = 'ws://193.112.175.120:8080/api/chat/'

  constructor(username: string) {
    this.websocket = new WebSocket(this.url + username)

    this.websocket.onopen = () => {
      console.log('WebSocket连接成功！')
    }

    this.websocket.onclose = () => {
      console.log('WebSocket连接关闭！')
    }

    this.websocket.onerror = (event) => {
      console.error('WebSocket连接出错！',event)
    }

    this.websocket.onmessage = (event) => {
      
      console.log('收到消息：', event.data)
    }
  }

  // 发送消息
  sendMsg(msg: string) {
    console.log('发送消息：', msg);
    
    if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
      this.websocket.send(msg)
    } else {
      console.error('WebSocket连接没有建立成功！')
    }
  }

  // 关闭连接
  close() {
    if (this.websocket) {
      this.websocket.close()
      console.log('WebSocket连接关闭！');
      
    }
  }
}


// export function createWebSocket(username: string) {

